import { execSync } from 'child_process';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import { dataGet } from '#Utils/helper.js';
const config = require('../../.sequelizerc');
import yargs from 'yargs';
import fs from 'fs';
import path from 'path';


// 读取并解析 .sequelizerc 文件内容


const template = `import DBConf from '#Config/database.js'
import { dataGet } from '#Utils/helper.js'

/** @type {import('sequelize-cli').Migration} */
export default {
    async up(queryInterface, Sequelize) {
        /**
         * Add altering commands here.
         *
         * Example:
         * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
         */
        const currentDatabase = queryInterface.sequelize.config.database;

        const publicDB = dataGet(DBConf, 'defaultDB');

        // if (currentDatabase !== publicDB) {
        //     return
        // }

        // 创建表
        // await queryInterface.createTable('users', {
        //     id: {
        //         type: Sequelize.INTEGER,
        //         primaryKey: true,
        //         autoIncrement: true,
        //     },
        //     createdAt: {
        //         type: Sequelize.DATE,
        //         allowNull: true,
        //         field: 'created_at',
        //     },
        //     updatedAt: {
        //         type: Sequelize.DATE,
        //         allowNull: true,
        //         field: 'updated_at',
        //     },
        //     deltetedAt: {
        //         type: Sequelize.DATE,
        //         allowNull: true,
        //         field: 'deleted_at',
        //     }
        // }, {
        //     timestamps: true,
        //     paranoid: true,
        // })

        // 添加字段
        // await queryInterface.addColumn('users', 'email', {
        //     type: Sequelize.STRING,
        //     allowNull: false,
        // })

        // 修改字段
        // await queryInterface.changeColumn('users', 'email', {
        //     type: Sequelize.STRING,
        //     allowNull: true,
        // })

        // 删除字段
        // await queryInterface.removeColumn('users', 'email')

        // 修改表名
        // await queryInterface.renameTable('users', 'user')


    },

    async down(queryInterface, Sequelize) {
        /**
         * Add reverting commands here.
         *
         * Example:
         * await queryInterface.dropTable('users');
         */
        const currentDatabase = queryInterface.sequelize.config.database;

        const publicDB = dataGet(DBConf, 'defaultDB');

        // if (currentDatabase !== publicDB) {
        //     return
        // }

        // 删除表
        // await queryInterface.dropTable('users')
        // 删除字段
        // await queryInterface.removeColumn('users', 'deletedAt')
    }
};`


const argv = yargs(process.argv.slice(2)).argv, name = dataGet(argv, 'name');

console.log(argv);


execSync(`npx sequelize-cli migration:generate --name=${name}`);

// 获取刚刚创建完成的迁移文件路径

// 获取最近创建的迁移文件
const migrationsPath = config['migrations-path'];
const migrationFiles = fs.readdirSync(migrationsPath);

// 排序迁移文件，确保获取最新的一个
const latestMigrationFile = migrationFiles.sort().reverse()[0];

// 获取迁移文件的完整路径
const migrationFilePath = path.join(migrationsPath, latestMigrationFile);

// 重写文件内容
fs.writeFileSync(migrationFilePath, template);
